package com.accenture.automation.util;

/**
 * 
 * 异常信息格式化
 * @author lihao.yin
 *
 */
public class ExceptionMsgUtil {
	public static String getErrorlog(Exception ex) {
        StringBuilder ebuf = new StringBuilder();
		StackTraceElement[] stels = ex.getStackTrace();
        ebuf.append(ex.toString());
        ebuf.append("\r\n");
        for (StackTraceElement stel : stels) {
            ebuf.append("    at " + stel.toString());
            ebuf.append("\r\n");
        }
        Throwable cause = null;
        for (int i = 0; i < 3; i++) {
            if (i == 0) {
                cause = ex.getCause();
            } else {
                cause = cause.getCause();
            }
            if (cause != null) {

                ebuf.append("Caused by: " + cause.toString());
                ebuf.append("\r\n");
                StackTraceElement[] stelsCause = cause.getStackTrace();
                int count = 0;
                int passCount = 0;
                for (StackTraceElement stelc : stelsCause) {
                    count++;
                    if (count < 6) {
                        ebuf.append("    at " + stelc.toString());
                        ebuf.append("\r\n");
                    } else {
                        passCount++;
                    }
                }
                if (passCount > 0) {
                    ebuf.append("... " +  passCount + " more");
                    ebuf.append("\r\n");
                }
            } else {
                i = 3;
            }
        }
        ebuf.append("\r\n");
		return ebuf.toString();
	}

}
